package com.itheima.controller.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.itheima.domain.Catalog;
import com.itheima.service.CatalogService;
import com.itheima.service.impl.CatalogServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/catalogServlet")
public class CatalogServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("application/json;charset=utf-8");
        String method = request.getParameter("method");
        Map resultMap = null;
        if ("findByPage".equals(method)) {
            resultMap = this.findByPage(request, response);
        } else if ("add".equals(method)) {
            resultMap = this.add(request, response);
        } else if ("update".equals(method)) {
            resultMap = this.update(request, response);
        } else if ("delete".equals(method)) {
            resultMap = this.delete(request, response);
        } else if ("findAll".equals(method)) {
            resultMap = this.findAll(request, response);
        }


        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(resultMap);
        response.getWriter().write(json);
    }

    private Map findAll(HttpServletRequest request, HttpServletResponse response) {
        Map resultMap = new HashMap();
        try {
            List<Catalog> list = catalogService.findAll();
            resultMap.put("flag", true);
            resultMap.put("resultData", list);
        } catch (Exception e) {
            resultMap.put("flag", false);
            resultMap.put("message", "查询失败!");
            e.printStackTrace();
        }
        return resultMap;
    }

    private CatalogService catalogService = new CatalogServiceImpl();

    private Map delete(HttpServletRequest request, HttpServletResponse response) {
        Map resultMap = new HashMap();
        try {
            String id = request.getParameter("catalogId");
            catalogService.delete(id);
            resultMap.put("flag", true);
        } catch (Exception e) {
            resultMap.put("flag", false);
            resultMap.put("message", "删除失败!");
            e.printStackTrace();
        }
        return resultMap;
    }

    private Map update(HttpServletRequest request, HttpServletResponse response) {
        Map resultMap = new HashMap();
        try {
            Map<String, String[]> map = request.getParameterMap();
            Catalog catalog = new Catalog();
            BeanUtils.populate(catalog, map);
            catalogService.update(catalog);
            resultMap.put("flag", true);
        } catch (Exception e) {
            resultMap.put("flag", false);
            resultMap.put("message", "修改失败!");
            e.printStackTrace();
        }
        return resultMap;
    }

    private Map add(HttpServletRequest request, HttpServletResponse response) {
        Map resultMap = new HashMap();
        try {
            Map<String, String[]> map = request.getParameterMap();
            Catalog catalog = new Catalog();
            BeanUtils.populate(catalog, map);
            catalogService.add(catalog);
            resultMap.put("flag", true);
        } catch (Exception e) {
            resultMap.put("flag", false);
            resultMap.put("message", "添加失败!");
            e.printStackTrace();
        }
        return resultMap;
    }

    private Map findByPage(HttpServletRequest request, HttpServletResponse response) {
        Map resultMap = new HashMap();
        try {
            String currentPageStr = request.getParameter("currentPage");
            int currentPage = 1;
            if (currentPageStr != null && !"".equals(currentPageStr)) {
                currentPage = Integer.parseInt(currentPageStr);
            }
            PageInfo info = catalogService.findByPage(currentPage);
            resultMap.put("flag", true);
            resultMap.put("resultData", info);
        } catch (Exception e) {
            resultMap.put("flag", false);
            resultMap.put("message", "查询失败!");
            e.printStackTrace();
        }
        return resultMap;
    }
}
